using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Configuration;

public partial class OpenQuickView : System.Web.UI.Page
{
    public Data.VarParams varParam = null;
    public Data.DataConsolidation dc = null;
    public Data.Data currentData = null;
    LoginHelper lh ;

    bool DoProcessProject( string sessionId, string physicalApplicationPath, bool IsPostBack, StateBag ViewState)
    {
        varParam = new Data.VarParams();
        String errMsg = varParam.ReadFile(physicalApplicationPath, sessionId);
        if (errMsg != null)
        {
            form1.Visible = false;
            Response.Write(errMsg);
            return false;
        }

        lh = new LoginHelper(this);
        if (IsPostBack == false)
            FilenameToSaveTo.Text = lh.GetProjectCaption();

        dc = new Data.DataConsolidation(varParam);

        currentData = new Data.Data();
        if (currentData == null)
            return false;

        bool result = DoProject.ExecuteProjectFile(lh.GetProjectCaption(), varParam, dc, currentData, IsPostBack);

        return result;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (false == DoProcessProject( Session.SessionID, Request.PhysicalApplicationPath, IsPostBack, ViewState))
            Session.Clear();

        else if (IsPostBack == false)
            Repaint();    // only provide the table on the first time in... after that, an event would have to ask for it.
    }

    private void Repaint()
    {
        AllPagesArea.Controls.Clear();

        if (currentData == null)
            return;

        MakeHtmlTable.makeHTMLTable(true, AllPagesArea, 1000, currentData, varParam );


        {
            ChartsArea.InnerHtml = "";

            Data.Page thisPage = currentData.GetSelectedPage();
            if (thisPage != null)
            {
                List<String> results = GoogleChart.getCharts( thisPage, true, true, true, true);
                foreach (String line in results)
                    ChartsArea.InnerHtml += line;
            }
        }
    }


    protected void ToExternalContentType(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string arg = btn.CommandArgument;

        /* If we can't get it looking right, then perhaps the next best thing is to redirect to a dynamically generated page, i.e. not "this" page
         */

        Response.Clear();
        Response.ContentType = arg;

        MyMenu.Visible = false;
        Repaint();
    }

    protected void HandleMenuItemClick(object sender, MenuEventArgs e)
    {
        string id = e.Item.Value;

        if (id.Equals("Author", StringComparison.CurrentCultureIgnoreCase))
        {
            string targetName = "OpenAuthorMode";
            Response.Redirect(lh.RedirectInfo(targetName + ".aspx", lh.GetProjectCaption()), true);
        }
        else if (id.Equals("ChooseProject", StringComparison.CurrentCultureIgnoreCase))
        {
            string targetName = "OpenChooseProject";
            Response.Redirect(lh.RedirectInfo(targetName + ".aspx", lh.GetProjectCaption()), true);
        }
        else if (id.StartsWith("application/", StringComparison.CurrentCultureIgnoreCase))
        {
            Response.Clear();
            MenuTable.Visible = false;
            Response.ContentType = id;
        }

        Repaint();
    }

}
